package org.testis.gui.mvc.controller.testdesign;

import org.testis.gui.mvc.controller.*;
import org.testis.gui.mvc.model.testdesign.TestSuiteModel;
import org.testis.gui.mvc.view.testdesign.TestSuiteView;
import java.util.HashMap;
import org.orm.PersistentException;
import org.testis.orm.refinedclasses.TestDesignTreeNode;
import org.testis.orm.refinedclasses.TestSuiteRefined;
import org.testis.orm.testdesign.TestPartSaver;
import org.testis.utils.middleware.TestDesign;
import org.testis.utils.tree.TreeNode;

/**
 *
 * @author Cahit Emir Erel
 */
public class TestSuiteController extends AbstractController implements SaveAble, Addable {

    public static final String SHOW_ADD_TEST_SUITE = "TestSuiteVisibility";
    public static final String CHANGE_NAME = "Name";
    public static final String CHANGE_DESCRIPTION = "Description";
    public static final String CHANGE_TIMEOUT = "Timeout";
    private TestDesignTreeNode m_selectedNode = null;

    public TestSuiteController() {
        super();
    }

    public boolean operationCheckTestSuiteNameUniqueness(String name,
            String exception) {
        if (exception != null && name.equals(exception)) {
            return true;
        } else {
            if (!TestDesign.newInstance().checkTestSuiteNameUniqueness(name)) {
                TestDesignControllerList.newInstance().getTreeController().
                        operationGenerateErrorMessage(
                        "This Test Suite name is already defined! Please type another name",
                        "ERROR");
                return false;
            } else {
                return true;
            }
        }
    }

    public void operationNewTestSuite() {
        initialize(false);
        m_selectedNode = TestDesignControllerList.newInstance().getTreeController().getSelectedTreeNode();
        setModelProperty(SHOW_ADD_TEST_SUITE,
                true);
    }

    public void initialize(boolean editMode) {
        TestSuiteModel testSuiteModel = new TestSuiteModel();
        TestSuiteView testSuiteView = new TestSuiteView(TestDesignControllerList.newInstance().
                getParentFrame(),
                true,
                editMode);
        super.addModel(testSuiteModel);
        super.addView(testSuiteView);
        testSuiteView.pack();
    }

    public void operationSaveTestSuite(String name,
            String description) {
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("name",
                name);
        params.put("description",
                description);
        TestDesignControllerList.newInstance().getTreeController().
                operationHandleSaveClones(this,
                m_selectedNode,
                params);
    }

    @Override
    public boolean operationSave(HashMap<String, Object> params,
            TreeNode node,
            boolean dynamicSaveOnly) {
        return TestPartSaver.operationSave(params,
                (TestDesignTreeNode) node);
    }

    public void operationAddTestSuite(String name,
            String description) throws PersistentException {

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("name",
                name);
        params.put("description",
                description);
        TestDesignControllerList.newInstance().getTreeController().operationHandleAdd(this, null, params);
    }

    public void operationEditTestSuite(TestDesignTreeNode node) {
        if (node.getReference().getType() == null) {
            System.out.println("Not found reference type for test suite");
            return;
        } else if (node.getReference().getType().getName() == null) {
            System.out.println(
                    "Not found reference type name for test suite");
            return;
        }
        if (node.getReference().getType().getName().equals("TestSuite")) {
            initialize(true);
            TestSuiteRefined designpart = null;
            try {
                designpart = (TestSuiteRefined) node.getObject();
            } catch (Exception ex) {
                System.out.println(ex.toString());
            }
            if (designpart == null) {
                System.out.println("Not found design part for test suite");
                return;
            }
            setModelProperty(CHANGE_NAME,
                    designpart.getName());
            setModelProperty(CHANGE_DESCRIPTION,
                    designpart.getReference().getDescription());
            m_selectedNode = node;
            setModelProperty(SHOW_ADD_TEST_SUITE,
                    true);
        }
    }

    public void operationChangeName(String name) {
        setModelProperty(CHANGE_NAME,
                name);
    }

    public void operationChangeDescription(String description) {
        setModelProperty(CHANGE_DESCRIPTION,
                description);
    }

    public void operationChangeTimeOut(String timeOut) {
        setModelProperty(CHANGE_TIMEOUT,
                timeOut);
    }

    public void operationCloseWindow() {
        cleanAll();
    }

    @Override
    public TestDesignTreeNode operationAddObject(HashMap<String, Object> params, TreeNode node, int childIndex) {
        TestDesignTreeNode nodeToAdd =  TestPartSaver.operationAdd(params,
                (TestDesignTreeNode) node, childIndex, "TestSuite");

        return nodeToAdd;
    }

    @Override
    public boolean isValidSave(HashMap<String, Object> params, TreeNode node) {
        return true;
    }

    @Override
    public boolean isValidAdd(HashMap<String, Object> params, TreeNode node) {
        return true;
    }
}
